package com.systop.party08.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.systop.party08.R;
import com.systop.party08.base.BaseFragment;
import com.systop.party08.entity.MyActivityEntity;
import com.systop.party08.http.MyStringCallback;
import com.systop.party08.utils.SPUtils;
import com.systop.party08.view.NestingListView;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.systop.party08.http.ComUrl.DJHD;
import static com.systop.party08.utils.StaticDateUtils.PAGE;
import static com.systop.party08.utils.StaticDateUtils.SP_TOKEN;

public class ActivityFragment extends BaseFragment {

    @BindView(R.id.activity_nlv)
    NestingListView nlv;
    @BindView(R.id.activity_refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.layout_no_data_tv)
    TextView tvNoData;

    private View view;
    private CommonAdapter<MyActivityEntity.PageBean.ListBean> adapter;
    private List<MyActivityEntity.PageBean.ListBean> list = new ArrayList<>();
    private int page = PAGE;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_activity, container, false);
        ButterKnife.bind(this, view);
        initLv();
        refresh.autoRefresh();
        return view;
    }

    private void getData() {
        if (view != null ) {
            page = PAGE;
            okhttp();
        }
    }

    private void initLv() {
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                okhttp();
            }
        });
        adapter = new CommonAdapter<MyActivityEntity.PageBean.ListBean>(getActivity(), R.layout.item_activity, list) {
            @Override
            protected void convert(ViewHolder viewHolder, MyActivityEntity.PageBean.ListBean item, int position) {
                viewHolder.setText(R.id.activity_tv_year, item.getNd() + "年");
                viewHolder.setText(R.id.activity_tv_title, item.getName());
                viewHolder.setText(R.id.activity_tv_date, item.getHdsj());
                viewHolder.setText(R.id.activity_tv_address, item.getHddd());
                TextView tvDes = viewHolder.getView(R.id.activity_tv_des);
                if (TextUtils.isEmpty(item.getHdjs())) {
                    tvDes.setVisibility(View.GONE);
                } else {
                    tvDes.setVisibility(View.VISIBLE);
                    tvDes.setText(item.getHdjs());
                }
                viewHolder.setText(R.id.activity_tv_num, "报名人数：" + item.getYbmrs() + "/" + item.getZdrs());
                TextView tv = viewHolder.getView(R.id.activity_tv_btn);
                tv.setVisibility(View.VISIBLE);
                tv.setText("已结束");
                tv.setTextColor(getResources().getColor(R.color.colorPrimary));
                tv.setBackgroundResource(android.R.color.white);
            }
        };
        nlv.setAdapter(adapter);
    }

    private void okhttp() {
        HashMap<String, String> param = new HashMap<>();
        param.put("token", SPUtils.getPreference(getActivity(), SP_TOKEN));
        param.put("pageRows", "3");
        param.put("pageNum", page + "");
        commonOkhttp.execute(DJHD, param, new MyStringCallback(getActivity(), false) {
            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                commonOkhttp.refreshComplete(page, refresh);
                MyActivityEntity result = gson.fromJson(response, MyActivityEntity.class);
                setData(result);
            }

            @Override
            public void onOther(String response, int status, String message) {
                super.onOther(response, status, message);
                commonOkhttp.refreshComplete(page, refresh);
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                commonOkhttp.refreshCompleteF(page, refresh);
            }
        });
    }

    private void setData(MyActivityEntity result) {
        if (view == null) return;
        List<MyActivityEntity.PageBean.ListBean> dataBeans = result.getPage().getList();
        if (page == PAGE) {
            //第一页 刷新
            list.clear();
            if (dataBeans != null && dataBeans.size() > 0) {
                //有数据
                list.addAll(dataBeans);
                ++page;
                tvNoData.setVisibility(View.GONE);
                if (refresh != null) {
                    refresh.setEnableLoadMore(true);
                }
            } else {
                if (refresh != null) {
                    refresh.setEnableLoadMore(false);
                    tvNoData.setVisibility(View.VISIBLE);
                }
            }
        } else {
            //加载下一页
            if (dataBeans != null && dataBeans.size() > 0) {
                list.addAll(dataBeans);
                ++page;
            } else {
                showMessage(getString(R.string.list_bottom));
            }
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}